﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;

using Styx;
using Styx.Common;
using Styx.CommonBot;
using Styx.CommonBot.Routines;

using Styx.Helpers;

using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using System.Windows.Media;
using System.Threading.Tasks;
using Styx.Pathing;
using KittyDru.Helpers;

namespace KittyDru
{
    internal class Utilities
    {
        #region Basic Variables
        private static LocalPlayer Me { get { return Styx.StyxWoW.Me; } }
        #endregion

        public static void log(string format, params object[] args) //use for slogging
        {
            Logging.Write(format, args);
        }

        public static void log(Color color, string format, params object[] args) //use for slogging
        {
            Logging.Write(color, format, args);
        }

        public static bool BusyCheck()
        {
            // Pause while casting, dead, mounted etc
            if (StyxWoW.Me.IsCasting || StyxWoW.Me.IsChanneling || SpellManager.GlobalCooldown || !StyxWoW.Me.IsAlive || StyxWoW.Me.Mounted || StyxWoW.Me.IsOnTransport)
                return true;
            return false;
        }

        internal static bool SafeFacing()
        {
            if (!Me.IsMoving && Me.GotTarget && Me.CurrentTarget.IsValid && Me.CurrentTarget.IsAlive && Unit.ValidUnit(Me.CurrentTarget) && !Me.IsSafelyFacing(Me.CurrentTarget))
            {
                Utilities.log(Colors.Orange, "Facing target: " + Me.CurrentTarget.Name);
                Me.CurrentTarget.Face();
                return true;
            }
            return false;
        }

        internal static bool SafeFacingBehind()
        {
            if (!Me.IsMoving && Me.GotTarget && Me.CurrentTarget.IsValid && Me.CurrentTarget.IsAlive && Unit.ValidUnit(Me.CurrentTarget) && !Me.IsSafelyBehind(Me.CurrentTarget) && Unit.MeleeDistance(Me.CurrentTarget) <= 10)
            {
                Utilities.log(Colors.Orange, "Go behind target: " + Me.CurrentTarget.Name);

                WoWPoint behindPoint =  CalculatePointBehindTarget();

                if(Navigator.CanNavigateFully(Me.Location, behindPoint)) {
                    Navigator.MoveTo((WoWPoint)behindPoint);
                    StopMoving.AtLocation((WoWPoint)behindPoint);
                    return true;
                }
    
                Me.Behind(Me.CurrentTarget);

                return true;
            }
            return false;
        }

        public static WoWPoint CalculatePointBehindTarget()
        {
            float facing = Me.CurrentTarget.Rotation;
            facing += WoWMathHelper.DegreesToRadians(180); // was 150 ?
            facing = WoWMathHelper.NormalizeRadian(facing);

            return Me.CurrentTarget.Location.RayCast(facing, 8 - 2f);
        }

    }
}
